package com.ttg.common.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ttg.common.enums.AccountDimTypeEnum;
import com.ttg.common.enums.IntervalTypeEnum;
import com.ttg.common.exception.BaseException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/9/23 15:16
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Data
public class TaskCostGradingDTO{

    /**
     * 统计类型 格式：month-按月,aggregate-累计
     * {@link com.ttg.common.enums.IntervalTypeEnum}
     */
    @JsonProperty("intervalType")
    private String intervalType;

    /**
     * 选择月份 格式：202508
     */
    @JsonProperty("date")
    private String date;

    /**
     * 选择机构
     */
    @JsonProperty("agentNo")
    private Long agentNo;

    //格式：2025
    private String year;

    //格式：08
    private String month;

//    private static final Pattern DATE_PATTERN = Pattern.compile("^\\d{4}(0[1-9]|1[0-2])$");
    private static final Pattern YEAR_PATTERN = Pattern.compile("^20\\d{2}$");
    private static final Pattern MONTH_PATTERN = Pattern.compile("^(0[1-9]|1[0-2])$");

    public String getYear(){
        if(StringUtils.isBlank(date)){
            return null;
        }
        validYear(date);
        return date.substring(0,4);
    }

    public String getMonth(){
        if(StringUtils.isBlank(date)){
            return null;
        }

        if(IntervalTypeEnum.年度区间.getValue().equals(intervalType)){
            return "";
        }
        validMonth(date);
        return date.substring(4,6);
    }

    public void validYear(String date){
        if(date.length()!=6){
            throw new BaseException("传入日期字符串长度错误");
        }
        if(!YEAR_PATTERN.matcher(date.substring(0,4)).matches()){
            throw new BaseException("传入格式错误：请传入20xxxx格式!,例如：202508");
        }
    }

    public void validMonth(String date){
        if(date.length()!=6){
            throw new BaseException("传入日期字符串长度错误");
        }
        if(!MONTH_PATTERN.matcher(date.substring(4,6)).matches()){
            throw new BaseException("传入格式错误：请传入20xxxx格式!,例如：202508");
        }
    }

    public static void main(String[] args) {
        TaskBaseDTO dto = new TaskBaseDTO();
        dto.setDate("202513");
        dto.getYear();
    }
}
